﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;

namespace WellDone
{
    public sealed partial class SplitMenuItemsDialog : Page
    {
        public event EventHandler CloseRequested;
        public int iCurrentSeat = 0;
        decimal decCurrentTotal = 0;
        public int iSplitCount = 0;
        public int iOrderNO = -1;
        public int iTableNO = -1;
        private ListView selectedcheck = null;

        bool bDiscountEntire=false, bDiscountEntirePercent=false;
        int iDiscountEntireAmount = 0, iDiscountEntireID = 0;

        public SplitMenuItemsDialog()
        {
            this.InitializeComponent();
        }

        public async Task<Boolean> addChecks(int CheckCount)
        {
            decCurrentTotal = 0;
            tbSplit.Text = CheckCount.ToString();
            int tmpCount = 0;
            while (tmpCount < CheckCount - 1)
            {
                await addCheck(tmpCount, false); // dynamically create new container for number of split checks
                tmpCount++;
            }
            return true;
        }

/*
        private void addCheck(int CheckCount)
        {
            addCheck(CheckCount, false);
        }
 */
        public void discountEntireOrder(bool bPercent, int iAmount, int iDiscountID)
        {
            bDiscountEntire = true;
            bDiscountEntirePercent = bPercent;
            iDiscountEntireAmount = iAmount;
            iDiscountEntireID = iDiscountID;
        }

        private async Task<Boolean> addCheck(int CheckCount, bool IsNewCheck)
        {
            try
            {
                Canvas tmpCanvas = new Canvas();
                tmpCanvas.Margin = new Thickness(((300 * CheckCount)), 0, 0, 0);

                Border tmpBorder = new Border();
                tmpBorder.BorderThickness = new Thickness(2);
                tmpBorder.BorderBrush = new SolidColorBrush(Colors.Black);
                tmpBorder.Background = new SolidColorBrush(Colors.LightGray);
                tmpBorder.HorizontalAlignment = HorizontalAlignment.Left;
                tmpBorder.VerticalAlignment = VerticalAlignment.Top;
                tmpBorder.Width = 300;
                tmpBorder.Height = 430;

                Grid GridSplitCheck = new Grid();
                GridSplitCheck.Margin = new Thickness(2);
                RowDefinition gridRow1 = new RowDefinition();
                gridRow1.Height = new GridLength(40);
                RowDefinition gridRow2 = new RowDefinition();
                gridRow2.Height = new GridLength(40);
                RowDefinition gridRow3 = new RowDefinition();
                RowDefinition gridRow4 = new RowDefinition();
                gridRow4.Height = new GridLength(40);

                GridSplitCheck.RowDefinitions.Add(gridRow1);
                GridSplitCheck.RowDefinitions.Add(gridRow2);
                GridSplitCheck.RowDefinitions.Add(gridRow3);
                GridSplitCheck.RowDefinitions.Add(gridRow4);

                /*create header strip row*/
                Grid GridSplitCheckHeaderstrip = new Grid();
                GridSplitCheckHeaderstrip.Background = new SolidColorBrush(Colors.LightGray);
                ColumnDefinition gridColstrip1 = new ColumnDefinition();

                GridSplitCheckHeaderstrip.ColumnDefinitions.Add(gridColstrip1);
                ColumnDefinition gridColstrip2 = new ColumnDefinition();
                gridColstrip2.Width = new GridLength(40);
                GridSplitCheckHeaderstrip.ColumnDefinitions.Add(gridColstrip2);
                TextBlock lblHeaderstrip = new TextBlock();
                lblHeaderstrip.Text = "Check # " + (CheckCount + 2);
                lblHeaderstrip.FontSize = 28;
                lblHeaderstrip.Margin = new Thickness(5, 4, 0, 0);
                Grid.SetColumn(lblHeaderstrip, 0);
                GridSplitCheckHeaderstrip.Children.Add(lblHeaderstrip);

                // Set remove button 
                Button btnRemobeHeaderstrip = new Button() { Content = "X", CommandParameter = CheckCount, Foreground = new SolidColorBrush(Colors.Red), FontSize = 20 };
                btnRemobeHeaderstrip.Tapped += btnRemobeHeaderstrip_Tapped;
                ToolTip toolTip = new ToolTip();
                toolTip.Content = "Remove check";
                ToolTipService.SetToolTip(btnRemobeHeaderstrip, toolTip);
                btnRemobeHeaderstrip.HorizontalContentAlignment = Windows.UI.Xaml.HorizontalAlignment.Left;
                btnRemobeHeaderstrip.Width = 60;
                btnRemobeHeaderstrip.Height = 40;
                Grid.SetColumn(btnRemobeHeaderstrip, 1);
                GridSplitCheckHeaderstrip.Children.Add(btnRemobeHeaderstrip);

                Border tmpBorderstrip = new Border();
                tmpBorderstrip.BorderThickness = new Thickness(2);
                tmpBorderstrip.BorderBrush = new SolidColorBrush(Colors.Black);

                tmpBorderstrip.Child = GridSplitCheckHeaderstrip;
                Grid.SetRow(tmpBorderstrip, 0);
                GridSplitCheck.Children.Add(tmpBorderstrip);

                /*create header row*/
                Grid GridSplitCheckHeader = new Grid();
                GridSplitCheckHeader.Background = new SolidColorBrush(Colors.LightGray);
                ColumnDefinition gridCol1 = new ColumnDefinition();
                ColumnDefinition gridCol2 = new ColumnDefinition();

                GridSplitCheckHeader.ColumnDefinitions.Add(gridCol1);
                GridSplitCheckHeader.ColumnDefinitions.Add(gridCol2);

                TextBlock lblHeader1 = new TextBlock();
                lblHeader1.Text = "Item";
                lblHeader1.FontSize = 28;
                lblHeader1.Margin = new Thickness(5, 4, 0, 0);
                Grid.SetColumn(lblHeader1, 0);

                TextBlock lblHeader2 = new TextBlock();
                lblHeader2.Text = "Price";
                lblHeader2.FontSize = 28;
                lblHeader2.HorizontalAlignment = HorizontalAlignment.Right;
                lblHeader2.Margin = new Thickness(0, 4, 15, 0);
                Grid.SetColumn(lblHeader2, 1);

                GridSplitCheckHeader.Children.Add(lblHeader1);
                GridSplitCheckHeader.Children.Add(lblHeader2);

                Grid.SetRow(GridSplitCheckHeader, 1);
                GridSplitCheck.Children.Add(GridSplitCheckHeader);

                /*create item list row*/
                ListView lstMenuItems = new ListView();
                lstMenuItems.HorizontalAlignment = HorizontalAlignment.Center;
                lstMenuItems.Height = 304;
                lstMenuItems.Margin = new Thickness(0);
                lstMenuItems.BorderThickness = new Thickness(0);
                lstMenuItems.Background = new SolidColorBrush(Colors.White);
                lstMenuItems.VerticalAlignment = VerticalAlignment.Top;
                lstMenuItems.Width = 296;
                lstMenuItems.ItemContainerStyle = this.Resources["ListViewItemStyleOpenRPOS"] as Style;
                lstMenuItems.IsSwipeEnabled = false;
                lstMenuItems.SelectionMode = ListViewSelectionMode.Multiple;
                lstMenuItems.Tag = CheckCount;
                lstMenuItems.SelectionChanged += lvMenuItems_SelectionChanged;
                lstMenuItems.ItemTemplate = lvMenuItems.ItemTemplate; //copy same itemtemplate as original / main list
                lstMenuItems.PointerPressed += lstMenuItems_PointerPressed; //attach event to allow to move selected items from main check to this check 

                if (IsNewCheck == false) // move menu items only when not adding from this page
                {
                    MoveItemsInChecks(lvMenuItems, lstMenuItems); //move menu items from main check to appropriate check
                }

                Grid.SetRow(lstMenuItems, 2);
                GridSplitCheck.Children.Add(lstMenuItems);


                /*create footer row*/
                Grid GridSplitCheckFooter = new Grid();
                GridSplitCheckFooter.Background = new SolidColorBrush(Colors.LightGray);
                gridCol1 = new ColumnDefinition();
                gridCol2 = new ColumnDefinition();

                GridSplitCheckFooter.ColumnDefinitions.Add(gridCol1);
                GridSplitCheckFooter.ColumnDefinitions.Add(gridCol2);

                lblHeader1 = new TextBlock();
                lblHeader1.Text = "Subtotal";
                lblHeader1.FontSize = 28;
                lblHeader1.Margin = new Thickness(5, 4, 0, 0);
                Grid.SetColumn(lblHeader1, 0);

                TextBlock lblOrderTotal = new TextBlock();
                decCurrentTotal = 0;
                decimal iDetailDiscountTotal = 0; // Discount  Total if discount is item wise 
                foreach (OrderData od in lstMenuItems.Items)
                {
                    if (od.MenuItemID > 0)
                    {
                        decCurrentTotal += od.Price;
                        iDetailDiscountTotal += (od.Price - await CommonUtility.CalculateDiscount(od.Price, od.DiscountID));  // Calculate detail discount 
                    }
                }

                decCurrentTotal -= iDetailDiscountTotal;
                if (bDiscountEntire)
                {
                    // Discount the entire check as specified
                    if (bDiscountEntirePercent)
                        decCurrentTotal -= (decimal)((double)decCurrentTotal * ((double)iDiscountEntireAmount / 100.0));
                    else
                        decCurrentTotal -= iDiscountEntireAmount;
                }

                lblOrderTotal.Text = decCurrentTotal.ToString("N");
                lblOrderTotal.FontSize = 28;
                lblOrderTotal.HorizontalAlignment = HorizontalAlignment.Right;
                lblOrderTotal.Margin = new Thickness(0, 4, 15, 0);
                Grid.SetColumn(lblOrderTotal, 1);

                GridSplitCheckFooter.Children.Add(lblHeader1);
                GridSplitCheckFooter.Children.Add(lblOrderTotal);

                Grid.SetRow(GridSplitCheckFooter, 3);
                GridSplitCheck.Children.Add(GridSplitCheckFooter);

                tmpBorder.Child = GridSplitCheck;
                tmpCanvas.Children.Add(tmpBorder);
                if (CheckCount > 1) SplitChecksContainer.Width = SplitChecksContainer.Width + 300;

                tmpCanvas.PointerPressed += tmpCanvas_PointerPressed;
                SplitChecksContainer.Children.Add(tmpCanvas);

                SplitChecksContainer.UpdateLayout();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                ResetContainer();
                iSplitCount = SplitChecksContainer.Children.Count;
                tbSplit.Text = Convert.ToString(iSplitCount);
            }
            return true;
        }

        private async void MoveItemsInChecks(ListView SenderListview, ListView ReceiverListview)
        {
            if (ReceiverListview == null || SenderListview == null) return; //return and do nothing, if source or destination list object is null or not create 
            int checkNo = Convert.ToInt32(ReceiverListview.Tag);
            List<object> selectedItems = new List<object>();
            foreach (OrderData od in SenderListview.Items)
            {
                if (od.Parent != null) continue;

                if (od.SeatNumber == checkNo + 1)
                {
                    od.SeatNumber = checkNo + 1;
                    ReceiverListview.Items.Add(od);
                    selectedItems.Add(od);
                    if (od.Options != null)
                    {
                        foreach (OrderData odt in od.Options)
                        {
                            ReceiverListview.Items.Add(odt);
                            od.SeatNumber = checkNo + 1;
                            selectedItems.Add(od);
                        }
                    }
                }
            }

            foreach (OrderData od in selectedItems)
            {
                if (od.Options != null)
                {
                    foreach (OrderData odt in od.Options)
                    {
                        SenderListview.Items.Remove(odt);
                    }
                }

                SenderListview.Items.Remove(od);
            }

            await recalculateTotal(lvMenuItems, -1);
            await recalculateTotal(ReceiverListview, checkNo);
            await recalculateTotal(SenderListview, checkNo);
        }

        void tmpCanvas_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            Canvas control = sender as Canvas;
            if (control == null)
                return;


            Border childBorder = ((Border)(control.Children[0]));

            ListView childHeaderListView = GetlistViewContainerFromAssociatedBorder(childBorder);
            if (childHeaderListView != null)
            {
                lstMenuItems_PointerPressed(childHeaderListView, null);
            }

        }

        void btnRemobeHeaderstrip_Tapped(object sender, TappedRoutedEventArgs e)
        {
            Button btnCross = sender as Button;
            if (btnCross != null) RemoveCheck(Convert.ToInt32(btnCross.CommandParameter));
        }

        /// <summary>
        /// Get ListView container from associated parent border 
        /// </summary>
        /// <param name="childBorder"></param>
        /// <returns></returns>
        private ListView GetlistViewContainerFromAssociatedBorder(Border childBorder)
        {
            if (childBorder == null) return null;

            if (childBorder.Child != null)
            {
                Grid childGrid = childBorder.Child as Grid;
                if (childGrid != null)
                {
                    return ((ListView)(((Panel)(childGrid)).Children[2]));
                }
            }
            return null;
        }

        private async void RemoveCheck(int chechNo)
        {
            try
            {
                Canvas selectedControl = SplitChecksContainer.Children[chechNo] as Canvas;

                if (selectedControl != null)
                {
                    Border childBorder = ((Border)(((Panel)(selectedControl)).Children[0]));
                    ListView childHeaderListView = GetlistViewContainerFromAssociatedBorder(childBorder);
                    if (childHeaderListView != null)
                    {
                        foreach (OrderData od in childHeaderListView.Items)
                        {
                            lvMenuItems.Items.Add(od);
                        }
                        await recalculateTotal(lvMenuItems, -1);
                    }
                }

                SplitChecksContainer.Children.Remove(selectedControl);

                UIElementCollection collection = SplitChecksContainer.Children;
                if (collection.Count > 1) SplitChecksContainer.Width = SplitChecksContainer.Width - 300;

                ResetContainer();

                iSplitCount -= 1;
                tbSplit.Text = Convert.ToString(iSplitCount);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                ResetContainer();
                iSplitCount = SplitChecksContainer.Children.Count;
                tbSplit.Text = Convert.ToString(iSplitCount);
            }
        }

        private void ResetContainer(bool fromOK = false)
        {
            try
            {
                UIElementCollection collection = SplitChecksContainer.Children;

                foreach (Canvas control in collection)
                {
                    control.Margin = new Thickness(((300 * SplitChecksContainer.Children.IndexOf(control))), 0, 0, 0);
                    Border childBorder = ((Border)(((Panel)(control)).Children[0]));

                    if (childBorder.Child != null)
                    {
                        Grid childGrid = childBorder.Child as Grid;
                        if (childGrid != null)
                        {
                            Border childHeaderBorder = ((Border)(((Panel)(childGrid)).Children[0]));
                            if (childHeaderBorder.Child != null)
                            {
                                Grid childHeaderGrid = childHeaderBorder.Child as Grid;
                                if (childHeaderGrid != null)
                                {
                                    ((TextBlock)childHeaderGrid.Children[0]).Text = "Check # " + (SplitChecksContainer.Children.IndexOf(control) + 2);
                                    ((Button)childHeaderGrid.Children[1]).CommandParameter = SplitChecksContainer.Children.IndexOf(control);
                                }
                            }

                            ListView childHeaderListView = ((ListView)(((Panel)(childGrid)).Children[2]));
                            if (childHeaderListView != null)
                            {
                                childHeaderListView.Tag = SplitChecksContainer.Children.IndexOf(control);
                                if (fromOK)
                                {
                                    foreach (OrderData od in childHeaderListView.Items)
                                    {
                                        od.SeatNumber = SplitChecksContainer.Children.IndexOf(control) + (lvMenuItems.Items.Count > 0 ? 1 : 0);
                                        if (od.Options != null)
                                        {
                                            foreach (OrderData odt in od.Options)
                                            {
                                                od.SeatNumber = SplitChecksContainer.Children.IndexOf(control) + (lvMenuItems.Items.Count > 0 ? 1 : 0);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                if (fromOK)
                {

                    foreach (OrderData od in lvMenuItems.Items)
                    {
                        od.SeatNumber = 0;
                        if (od.Options != null)
                        {
                            foreach (OrderData odt in od.Options)
                            {
                                od.SeatNumber = 0;
                            }
                        }
                    }
                }

                SplitChecksContainer.UpdateLayout();
            }
            catch { }
        }

        private async void lstMenuItems_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            try
            {
                ListView lstMenuItems = (ListView)sender;
                if (lstMenuItems != null && selectedcheck != null)
                {
                    if (lstMenuItems.Tag != null && selectedcheck.Tag != null && Convert.ToInt32(lstMenuItems.Tag) == Convert.ToInt32(selectedcheck.Tag)) return;

                    List<object> selectedItems = new List<object>();
                    foreach (OrderData od in selectedcheck.SelectedItems)
                    {
                        lstMenuItems.Items.Add(od);
                        selectedItems.Add(od);
                        if (od.Options != null)
                        {
                            foreach (OrderData odt in od.Options)
                            {
                                lstMenuItems.Items.Add(odt);
                                selectedItems.Add(od);
                            }
                        }
                    }

                    foreach (OrderData od in selectedItems)
                    {
                        if (od.Options != null)
                        {
                            foreach (OrderData odt in od.Options)
                            {
                                selectedcheck.Items.Remove(odt);
                            }
                        }
                        selectedcheck.Items.Remove(od);
                    }

                    await recalculateTotal(lvMenuItems, -1);
                    await recalculateTotal(lstMenuItems, Convert.ToInt32(lstMenuItems.Tag));
                    await recalculateTotal(selectedcheck, Convert.ToInt32(selectedcheck.Tag));
                }
            }
            catch { }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            // remove check container if it has no item
            RemoveBlankContainer(true);

            // Send close request    
            if (this.CloseRequested != null)
            {
                this.CloseRequested(this, EventArgs.Empty);
            }
        }

        public void setHeaderText(string strHeaderText)
        {
            // Set the header text
            tblkHeader.Text = strHeaderText;
            lblOrderNo.Text = Convert.ToString(iOrderNO);
            lblTableNo.Text = Convert.ToString(iTableNO);
        }

        public void addOrderDetailItem(OrderData od)
        {
            // Add passed item to the list
            //od.SeatNumber = 0;
            lvMenuItems.Items.Add(od);
            lvMenuItems.ScrollIntoView(od);
            decCurrentTotal += od.Price;
            lblOrderTotal.Text = decCurrentTotal.ToString("N");
        }

        private async Task<Boolean> recalculateTotal(ListView lstItems, int checkCount)
        {
            decimal decLocalCurrentTotal = 0;
            decimal iDetailDiscountTotal = 0; // Discount  Total if discount is item wise 
            foreach (OrderData od in lstItems.Items)
            {
                if (od.MenuItemID > 0)
                {
                    decLocalCurrentTotal += od.Price;
                    if (od.DiscountID != iDiscountEntireID)
                        iDetailDiscountTotal += (od.Price - await CommonUtility.CalculateDiscount(od.Price, od.DiscountID));  // Calculate detail discount 
                }
            }
            decLocalCurrentTotal -= iDetailDiscountTotal;
            if (bDiscountEntire)
            {
                // Discount the entire check as specified
                if (bDiscountEntirePercent)
                    decLocalCurrentTotal -= (decimal)((double)decLocalCurrentTotal * ((double)iDiscountEntireAmount / 100.0));
                else
                    decLocalCurrentTotal -= iDiscountEntireAmount;
            }
            if (checkCount >= 0)
            {
                try
                {
                    Grid childFooterGrid = ((Grid)(((Panel)((Grid)lstItems.Parent)).Children[3]));
                    if (childFooterGrid != null)
                    {
                        TextBlock lblTotal = ((TextBlock)childFooterGrid.Children[1]) as TextBlock;
                        if (lblTotal != null) lblTotal.Text = decLocalCurrentTotal.ToString("N");
                    }
                }
                catch { }
            }
            else
            {
                lblOrderTotal.Text = decLocalCurrentTotal.ToString("N");
            }
            return true;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            // remove check container if it has no item
            RemoveBlankContainer();

            this.CloseRequested(this, null);
        }

        private void lvMenuItems_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListView lstMenuItems = (ListView)sender;
            if (lstMenuItems != null)
            {
                if (selectedcheck != null && lstMenuItems.Tag != null && Convert.ToInt32(selectedcheck.Tag) != Convert.ToInt32(lstMenuItems.Tag)) selectedcheck.SelectedItems.Clear();

                selectedcheck = lstMenuItems;

                if (lstMenuItems.SelectedItems.Count > 0)
                {
                    foreach (OrderData od in lstMenuItems.SelectedItems)
                        // Do not allow to select options from the list...
                        if (od.Parent != null) lstMenuItems.SelectedItems.Remove(od);
                }
            }
        }

        private async void btnAddCheck_Click(object sender, RoutedEventArgs e)
        {
            await addCheck(iSplitCount - 1, true);
            iSplitCount = iSplitCount + 1;
            tbSplit.Text = iSplitCount.ToString();
        }

        private void brdMainCheck_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            Border childBorder = sender as Border;

            ListView childHeaderListView = GetlistViewContainerFromAssociatedBorder(childBorder);
            if (childHeaderListView != null)
            {
                lstMenuItems_PointerPressed(childHeaderListView, null);
            }
        }

        private void RemoveBlankContainer(bool fromOk = false)
        {
            try
            {
                UIElementCollection collection = SplitChecksContainer.Children;
                List<int> lstContainerwithoutItem = new List<int>();
                List<int> lstContainerwithItem = new List<int>();
                foreach (Canvas control in collection)
                {
                    control.Margin = new Thickness(((300 * SplitChecksContainer.Children.IndexOf(control))), 0, 0, 0);
                    Border childBorder = ((Border)(((Panel)(control)).Children[0]));

                    ListView childHeaderListView = GetlistViewContainerFromAssociatedBorder(childBorder);
                    if (childHeaderListView != null)
                    {
                        if (childHeaderListView.Items.Count <= 0)
                        {
                            lstContainerwithoutItem.Add(SplitChecksContainer.Children.IndexOf(control)); //Remove Container if no item 
                        }
                        else if (fromOk == false && childHeaderListView.Items.Count > 0) // check if move some item with cancel 
                        {
                            bool isSameItemExists = false;
                            foreach (OrderData od in childHeaderListView.Items)
                            {
                                if (od.Parent != null) continue;

                                if (od.SeatNumber == Convert.ToInt32(childHeaderListView.Tag) + 1)
                                {
                                    isSameItemExists = true;

                                }
                                else if (lstContainerwithItem.Contains(od.SeatNumber - 1) == false) // if list contains other split check item  on cancel 
                                {
                                    lstContainerwithItem.Add(od.SeatNumber - 1);
                                }
                            }

                            if (isSameItemExists == false)
                            {
                                lstContainerwithoutItem.Add(SplitChecksContainer.Children.IndexOf(control)); // remove item if there is no item related it's  number 
                            }
                        }
                    }
                }

                lstContainerwithoutItem.Reverse();
                foreach (int iIndexofContainer in lstContainerwithoutItem)
                {
                    if (fromOk == false && (lstContainerwithItem.Contains(iIndexofContainer) || IsExistsInMain(iIndexofContainer))) continue; //   on cancel ,  it is in either main check or other real exists split check 

                    SplitChecksContainer.Children.RemoveAt(iIndexofContainer);

                    iSplitCount -= 1;
                    tbSplit.Text = Convert.ToString(iSplitCount);
                }

                if (fromOk && lvMenuItems.Items.Count <= 0) // if there is no item in main 
                {
                    iSplitCount -= 1;
                    tbSplit.Text = Convert.ToString(iSplitCount);
                }
                else if (fromOk == false && lvMenuItems.Items.Count > 0)
                {
                    foreach (OrderData od in lvMenuItems.Items)
                    {
                        if (od.Parent != null) continue;

                        if (od.SeatNumber != 0 && iSplitCount <= od.SeatNumber) iSplitCount += 1; // increment with 1 when split check is less than main check items seat number 

                    }
                }

                if (fromOk == false && lstContainerwithItem.Count > 0)
                {
                    foreach (int indexContainer in lstContainerwithItem)
                    {
                        if (iSplitCount < indexContainer + 2) iSplitCount += 1; // increment with one when items of  exists container  in main check 
                    }
                }

                ResetContainer(fromOk);
            }
            catch { }
        }

        /// <summary>
        /// check if  split check index is present in main check 
        /// </summary>
        /// <param name="iIndexofContainer"></param>
        /// <returns></returns>
        private bool IsExistsInMain(int iIndexofContainer)
        {
            foreach (OrderData od in lvMenuItems.Items)
            {
                if (od.Parent != null) continue;

                if (od.SeatNumber == iIndexofContainer + 1)
                    return true;
            }
            return false;
        }

    }
}
